<template>
  <!-- 工作排班增加支援医生 -->
  <div class>
    <el-dialog :title="optTitle" :visible.sync="visible" width="700px" :destroy-on-close="true" :before-close="handleClose">
      <el-card class="box-card apply-card-pop">
        <div class="form-wrap">
          <el-form :inline="true" :model="listQuery" class="demo-form-inline top-form">
            <el-form-item label="姓名">
              <el-input clearable size="small" v-model="listQuery.name" placeholder="请输入"></el-input>
            </el-form-item>
            <el-form-item size="small" label="小组">
              <el-select
                v-model="listQuery.group"
                filterable
                clearable
                default-first-option
                placeholder="请选择"
              >
                <el-option
                  v-for="(item,index) in computedGroups"
                  :key="index"
                  :disabled="item.id==gid"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="级别">
              <el-select v-model="listQuery.level" clearable size="small">
                <el-option
                  v-for="(item,index) in levelOptions"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="small" type="primary" icon="el-icon-search" @click="search">查询</el-button>
            </el-form-item>
          </el-form>

          <el-card shadow="hover">
            <div class="table-list">
              <span
                class="table-item"
                @click="selectItem(index,item)"
                v-for="(item,index) in tableList"
                :key="index"
              >
                <el-tag v-if="!item.selected" type="primary" effect="plain">{{item.userName}}</el-tag>
                <el-tag v-else type="primary" effect="dark">{{item.userName}}</el-tag>
              </span>
            </div>
          </el-card>
        </div>
      </el-card>

      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="handleConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getUserListAll } from '@/api/api-rostering.js'
import publicDic from '@/utils/publicDic'

export default {
  extends: publicDic,
  components: {
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    sarr: { // 已选支援
      type: Array
    },
    // fromNight: {
    //   type: Boolean,
    //   default: false
    // },
    tit: {
      type: String
    },
    info: {
      type: Object
    },
    groups: { // 小组列表
      type: Array
    },
    gid: { // 当前小组id
      type: String
    }
  },
  data() {
    return {
      group: '',
      computedGroups: [],
      optTitle: '添加支援医生',
      selectedList: [],
      tableList: [],
      listQuery: {
        name: '',
        group: '',
        level: ''
      },
      date: ''
    }
  },
  watch: {
    visible(val) {
      this.resetData()
      // console.log(this.info.event.title)
      if (val) {
        let i = this.groups.findIndex(item => item.id == this.gid)
        this.group = this.groups[i].name
        this.optTitle = `添加 ${this.tit} ${this.groups[i].name} 支援医生`
        this.getUserListAll()
      }
    },
    info(val) {
      // this.listQuery.sex = val.sex
      // this.listQuery.ifConceived = val.ifConceived
      // this.listQuery.ifNight = val.isNightShift
      // this.listQuery.level = val.level
      // // time: [dateFormatter(new Date(new Date() - 3600 * 1000 * 24 * 7), false), dateFormatter(new Date(), false)],
      // this.listQuery.name = val.userName
      // this.listQuery.phone = val.phone
      // this.listQuery.birthDate = val.birthDate
    }
  },
  computed: {

  },
  methods: {
    getUID() { // 获取唯一值
      return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0; var v = c == 'x' ? r : (r & 0x3 | 0x8)
        return v.toString(16)
      })
    },
    getAge(strAge) {
      var birArr = strAge.split('/')
      var birYear = birArr[0]
      var birMonth = birArr[1]
      var birDay = birArr[2]

      d = new Date()
      var nowYear = d.getFullYear()
      var nowMonth = d.getMonth() + 1 // 记得加1
      var nowDay = d.getDate()
      var returnAge

      if (birArr == null) {
        return false
      }
      var d = new Date(birYear, birMonth - 1, birDay)
      if (d.getFullYear() == birYear && (d.getMonth() + 1) == birMonth && d.getDate() == birDay) {
        if (nowYear == birYear) {
          returnAge = 0 //
        } else {
          var ageDiff = nowYear - birYear //
          if (ageDiff > 0) {
            if (nowMonth == birMonth) {
              var dayDiff = nowDay - birDay //
              if (dayDiff < 0) {
                returnAge = ageDiff - 1
              } else {
                returnAge = ageDiff
              }
            } else {
              var monthDiff = nowMonth - birMonth //
              if (monthDiff < 0) {
                returnAge = ageDiff - 1
              } else {
                returnAge = ageDiff
              }
            }
          } else {
            return '出生日期晚于今天，数据有误' // 返回-1 表示出生日期输入错误 晚于今天
          }
        }
        return returnAge
      } else {
        return ('输入的日期格式错误！')
      }
    },
    getUserListAll() {
      let data = {
        username: this.listQuery.name,
        groupid: this.listQuery.group,
        userType: this.listQuery.level
      }
      getUserListAll(data).then(res => {
        if (!res) return
        let list = []
        res.result.forEach((item, index, arr) => {
          if (item.groupId != this.gid) {
            // console.log('dddd')
            let i = this.sarr.findIndex(sitem => sitem == item.userName)
            if (i < 0) {
              list.push(item)
            }
          }
        })
        this.tableList = list
      })
    },
    search() {
      this.tableList = []
      this.getUserListAll()
    },
    selectItem(index, item) {
      // console.log('dddd', item)
      // this.tableList.forEach((p) => {
      //   p.selected = false
      // })
      item.selected = !item.selected
      if (item.selected) {
        this.selectedList.push(item)
      } else {
        let i = this.selectedList.findIndex(sitem => sitem.id == item.id)
        this.selectedList.splice(i, 1)
      }
      this.tableList.splice(index, 1, item)
    },
    handleDelete() {
      this.$emit('confirmDelete', this.date)
      this.resetData()
    },
    handleClose() {
      this.$emit(`update:visible`, false)
    },
    handleConfirm() {
      // let sarr = []
      // for (let item of this.tableList) {
      //   if (item.selected) {
      //     sarr.push(item)
      //   }
      // }
      this.$emit('confirmEdit', this.selectedList)
      this.handleClose()
    },
    resetData() {
      this.listQuery.group = ''
      // 当前小组之外的其他小组成员
      this.computedGroups = JSON.parse(JSON.stringify(this.groups))

      this.listQuery.level = ''
      this.listQuery.name = ''
      this.tableList = []
    }
  },
  mounted() {
  },
  created() {
  }
}
</script>

<style lang="scss" scoped>
.el-dialog__footer {
  text-align: center;
}
.apply-card-pop {
  >>> .el-card__body {
    padding: 20px;
    >>> .apply-card .el-input .el-input__inner {
      width: inherit;
    }
  }
}
.apply-card-pop >>> .el-select {
  width: 120px;
}
.apply-card-pop >>> .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 120px;
}
.apply-card-pop >>> .el-input.el-input--small.el-input--suffix {
  width: 120px;
}
.table-list {
  min-height: 100px;
  .table-item {
    display: inline-block;
    cursor: pointer;
    // border: 1px solid #95e7fb;
    margin-right: 10px;
    // padding: 5px 8px;
    // border-radius: 5px;
    margin-bottom: 10px;
    // color: #3d4d66;
    // background-color: #EAF2FD;
    // font-weight: normal;
    // &.cannot {
    // }
  }
}
</style>
